package com.kemai.designmode.ui
import android.os.Bundle
import com.kemai.common.adapter.ItemClickListener
import com.kemai.common.ui.BaseActivity
import com.kemai.designmode.R
import com.kemai.designmode.factory.abstract.AbstractFactory
import com.kemai.designmode.factory.abstract.AppleAbstractFactory
import com.kemai.designmode.factory.abstract.BananaAbstractFactory
import com.kemai.designmode.factory.abstract.OrangeAbstractFactory
import com.kemai.designmode.entry.Fruit
import com.kemai.designmode.factory.method.AppleMethodFactory
import com.kemai.designmode.factory.method.BananaMethodFactory
import com.kemai.designmode.factory.method.OrangeMethodFactory

import com.kemai.designmode.visit.Visit



//访问者模式调用(利用双重分派机制，弥补java多态中方法重载静态化不足，
// 比如集合中有多个类型对象，通过方法重载(形参)无法识别他们具体的类型
// 通过双重委派，可以实现遍历识别，达到目的)
class VisitActivity : BaseActivity(), ItemClickListener {
    private val studyContent = arrayOf("计算水果价格")
    val visit = Visit()
    private val list: MutableList<Fruit> = ArrayList() //库存
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_common_content)
        findViewById()
        setTiTle("访问者模式")
        setAdapterContent(studyContent,VisitActivity@this,VisitActivity@this)
    }

    override fun itemClick(position: Int) {
        list.add(AppleMethodFactory().getFruit())
        list.add(OrangeMethodFactory().getFruit())
        list.add(BananaMethodFactory().getFruit())
        when(position){
            0->{
               for (i in 0 until list.size) {
                   val fruit = list[i]
                   fruit.acpept(visit)
                   //visit.sell(fruit) 该方法不能识别真实对象
               }

            }

        }


    }

}